package cn.com.main;

import cn.com.utils.MachineTranslationUtil;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;

public class GenerateMenuPermissions {
    public static final String TYPE = "{type}";
    public static final String GROUP_SID = "{group_sid}";

    public static void main(String[] args) {

        //菜单权限
        Map<String, String> menuMap = new TreeMap<>();
        menuMap.put("CH08", "临时人员授权");
        menuMap.put("CH09", "我的团队");
        menuMap.put("CH0901", "创建课题");
        menuMap.put("CH0902", "编辑课题");
        menuMap.put("CH0903", "解散课题");
        menuMap.put("CH0904", "老师切换课题");
        menuMap.put("CH0905", "查询课题详情");
        menuMap.put("CH0906", "教师账号下所有课题查询");
        menuMap.put("CH0907", "课题验证接囗");
        menuMap.put("CH0908", "可以邀请的学生列表查询");
        menuMap.put("CH0909", "邀请历史查询");
        menuMap.put("CH0910", "邀请学生");
        menuMap.put("CH0911", "查询老师的课题成员");
        menuMap.put("CH0912", "移除成员");
        menuMap.put("CH0913", "查询申请");
        menuMap.put("CH0914", "申请处理");
        menuMap.put("CH0915", "查询课题列表");
        menuMap.put("CH0916", "发起申请");
        menuMap.put("CH0917", "查询申请历史");
        menuMap.put("CH0918", "撤销申请");
        menuMap.put("CH0919", "查询当前学生所有课题");
        menuMap.put("CH0920", "学生切换课题");
        menuMap.put("CH0921", "查询当前课题详情");
        menuMap.put("CH0922", "退出课题");
        menuMap.put("CJ", "课题管理");
        menuMap.put("CJ01", "老师查询总配额");
        menuMap.put("CJ02", "老师查询课题配额列表");
        menuMap.put("CJ03", "老师批量修改课题配额");
        menuMap.put("BJ", "配额管理");
        menuMap.put("BJ01", "配额创建");
        menuMap.put("BJ02", "管理员查询总配额列表");
        menuMap.put("BJ03", "管理员修改配额");
        menuMap.put("BJ04", "查询老师列表");

        String template = "        // %s\n" +
                "        String %s = \"%s\";";


        StringBuilder sb = new StringBuilder();
        menuMap.forEach((menuCode, menuName) -> {
            sb.append(String.format(template, menuName, menuCode, menuCode));
            sb.append("\n");
        });
        System.out.println(sb.toString());
    }

    private static void assertion() {
        //菜单权限
        Map<String, String> menuMap = new TreeMap<>();
        menuMap.put("CH08", "临时人员授权");
        menuMap.put("CH09", "我的团队");
        menuMap.put("CH09:CH0901", "创建课题");
        menuMap.put("CH09:CH0902", "编辑课题");
        menuMap.put("CH09:CH0903", "解散课题");
        menuMap.put("CH09:CH0904", "老师切换课题");
        menuMap.put("CH09:CH0905", "查询课题详情");
        menuMap.put("CH09:CH0906", "教师账号下所有课题查询");
        menuMap.put("CH09:CH0907", "课题验证接囗");
        menuMap.put("CH09:CH0908", "可以邀请的学生列表查询");
        menuMap.put("CH09:CH0909", "邀请历史查询");
        menuMap.put("CH09:CH0910", "邀请学生");
        menuMap.put("CH09:CH0911", "查询老师的课题成员");
        menuMap.put("CH09:CH0912", "移除成员");
        menuMap.put("CH09:CH0913", "查询申请");
        menuMap.put("CH09:CH0914", "申请处理");
        menuMap.put("CH09:CH0915", "查询课题列表");
        menuMap.put("CH09:CH0916", "发起申请");
        menuMap.put("CH09:CH0917", "查询申请历史");
        menuMap.put("CH09:CH0918", "撤销申请");
        menuMap.put("CH09:CH0919", "查询当前学生所有课题");
        menuMap.put("CH09:CH0920", "学生切换课题");
        menuMap.put("CH09:CH0921", "查询当前课题详情");
        menuMap.put("CH09:CH0922", "退出课题");
        menuMap.put("CJ", "课题管理");
        menuMap.put("CJ:CJ01", "老师查询总配额");
        menuMap.put("CJ:CJ02", "老师查询课题配额列表");
        menuMap.put("CJ:CJ03", "老师批量修改课题配额");
        menuMap.put("BJ", "配额管理");
        menuMap.put("BJ:BJ01", "配额创建");
        menuMap.put("BJ:BJ02", "管理员查询总配额列表");
        menuMap.put("BJ:BJ03", "管理员修改配额");
        menuMap.put("BJ:BJ04", "查询老师列表");

        //学生权限
        Set<String> studentRights = new HashSet<>();
        studentRights.add("查询课题列表");
        studentRights.add("发起申请");
        studentRights.add("查询申请历史");
        studentRights.add("撤销申请");
        studentRights.add("查询当前学生所有课题");
        studentRights.add("学生切换课题");
        studentRights.add("查询当前课题详情");
        studentRights.add("退出课题");


        String sys_m_assertion = " ('" + TYPE + "', '%s', '%s', 'ModelMateService', 'ModelMate', 'ModelMate', 'ModelMate', '*', " +
                "'allow', 1, '%s', 0, %s, 'admin', '2023-05-19 03:34:24', 'admin', '2023-05-19 03:34:24', 1, NULL),";
        String sys_m_policy_assertion = "((select assertion_sid from sys_m_assertion where op_code = '%s'), " + GROUP_SID + ", '{\\\"ModelMate\\\": \\\"%s\\\"}', 'platform'),";

        StringBuilder sys_m_assertion_sql = new StringBuilder("INSERT INTO `sys_m_assertion` (`assertion_type`, `op_name`, `op_code`, `service_code`, `service_name`, `resource_code`,\n" +
                "                               `resource_name`, `resource`, `effect`, `status`, `module_sid`, `is_default`, `sort_rank`,\n" +
                "                               `created_by`, `created_dt`, `updated_by`, `updated_dt`, `version`, `relate_path`)\n" +
                "VALUES ");
        StringBuilder sys_m_policy_sql = new StringBuilder("INSERT INTO `sys_m_policy_assertion` (`assertion_sid`, `policy_sid`, `assertion_expression`, `type`)\n" +
                "VALUES ");
        TreeSet<String> englishs = new TreeSet<>();
        AtomicInteger sort = new AtomicInteger(500);
        menuMap.forEach((menuCode, menuName) -> {
            String english = MachineTranslationUtil.toEnglish(menuName);
            english = english.trim().replaceAll("'", "");
            english = toCamelCase(english);
            englishs.add("'" + english + "'");

            String sql = String.format(sys_m_assertion, menuName, english, menuCode, sort.getAndIncrement());
            sql = sql.replace(TYPE, menuCode.length() <= 4 ? "view" : "mgt");
            sys_m_assertion_sql.append(sql);
            sql = String.format(sys_m_policy_assertion, english, english);
            sql = sql.replace(GROUP_SID, studentRights.contains(menuName) ? "99" : "98");
            sys_m_policy_sql.append(sql);
        });
        System.out.println("\n\n+++++++++++++++++\n\n");
        System.out.println(sys_m_assertion_sql.toString());

        System.out.println("\n\n+++++++++++++++++\n\n");
        System.out.println(sys_m_policy_sql.toString());


        System.out.println("\n\n+++++++++++++++++\n\n");
        System.out.println(englishs.toString());
    }

    public static String toCamelCase(String input) {
        StringBuilder result = new StringBuilder();
        String[] parts = input.split(" ");
        for (int i = 0; i < parts.length; i++) {
            if (i == 0) {
                result.append(parts[i]);
            } else {
                result.append(capitalize(parts[i]));
            }
        }
        return result.toString();
    }

    public static String capitalize(String word) {
        return Character.toUpperCase(word.charAt(0)) + word.substring(1);
    }

    private static void menuPermission() {
        String prentMenuCode = "CH09";

        String menuCode = "CH090";

        //接口名称
        //创建课题
        //编辑课题
        //解散课颖
        //切换课题
        //查询课题详情
        //教师账号下所有课题查询
        //课题验证接囗
        //可以邀请的学生列表查询
        //邀请历史查询
        //邀请学生
        //查询老师的课题成员
        //移除成员
        //查询申请
        //申请处理
        //查询课题列表
        //发起申请
        //查询申请历史
        //撤销申请
        //查询当前学生所有课题
        //切换课题
        //查询当前课题详情
        //退出课题

        String menuName = "创建课题,编辑课题,解散课题,切换课题,查询课题详情,教师账号下所有课题查询,课题验证接囗,可以邀请的学生列表查询,邀请历史查询,邀请学生,查询老师的课题成员,移除成员,查询申请,申请处理,查询课题列表,发起申请,查询申请历史,撤销申请,查询当前学生所有课题,切换课题,查询当前课题详情,退出课题";
        String sqlTemplate = "('%s', '%s', null, null, '%s', null, 2, 0, %s, 'mgt', ''),";
        String[] split = menuName.split(",");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String menuNameItem = split[i];
            String sql = String.format(sqlTemplate, menuCode + (i + 1), menuNameItem, prentMenuCode, i + 1);
            stringBuilder.append(sql);
        }
        System.out.println(stringBuilder.toString());
    }
}
